package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.CourseTeacher;
import com.example.result.Result;
import com.example.service.CourseTeacherService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 老师课程控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class CourseTeacherController {

    @Autowired
    CourseTeacherService courseTeacherService;

    // ===============================老师课程管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加老师课程信息
     */
    @RequestMapping(value = "/courseTeachers", method = RequestMethod.POST)
    public Result addCourseTeacher(@RequestBody String json) {
        return courseTeacherService.addCourseTeacher(json);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新老师课程信息
     */
    @RequestMapping(value = "/courseTeachers", method = RequestMethod.PUT)
    public Result updateCourseTeacher(@RequestBody String json) {
        // 将JSON数据转换为CourseTeacher对象实体
        JSONObject parseObject = JSON.parseObject(json);
        CourseTeacher courseTeacher = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"),
                CourseTeacher.class);
        return courseTeacherService.updateCourseTeacher(courseTeacher);

    }

    /**
     * @param courseTeacherId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据老师课程编号删除老师课程信息
     */
    @RequestMapping(value = "/courseTeachers/{courseTeacherId}", method = RequestMethod.DELETE)
    public Result deleteCourseTeacherById(@PathVariable("courseTeacherId") String courseTeacherId) {
        return courseTeacherService.deleteCourseTeacherById(courseTeacherId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有老师课程信息
     */
    @RequestMapping(value = "/courseTeachers", method = RequestMethod.GET)
    public Result queryAllCourseTeacher() {
        return courseTeacherService.queryAllCourseTeacher();

    }

    /**
     * @param courseTeacherId
     * @return 以JSON格式返回该老师课程信息
     * @describe 根据编号查询老师课程信息
     */
    @RequestMapping(value = "/courseTeachers/{courseTeacherId}", method = RequestMethod.GET)
    public Result queryCourseTeacherById(@PathVariable("courseTeacherId") String courseTeacherId) {
        return courseTeacherService.queryCourseTeacherById(courseTeacherId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加老师课程信息
     */
    @RequestMapping(value = "/courseTeachers/excels", method = RequestMethod.POST)
    public Result addCourseTeacherByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<CourseTeacher> list = JSON.parseArray(parseObject.getString("json"), CourseTeacher.class);
        return courseTeacherService.addCourseTeacherByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据老师课程编号批量删除老师课程信息
     */
    @RequestMapping(value = "/courseTeachers/dels", method = RequestMethod.POST)
    public Result deleteCourseTeacherByIds(@RequestBody Map<String, String> Ids) {

        return courseTeacherService.deleteCourseTeacherByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 老师课程信息模糊查询
     */
    @RequestMapping(value = "/courseTeachers/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return courseTeacherService.serachByInfo(info);
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有未分配课程的老师
     */
    @RequestMapping(value = "/courseTeachers/teachers", method = RequestMethod.GET)
    public Result queryAllTeachers() {
        return courseTeacherService.queryAllTeachers();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有课程名称
     */
    @RequestMapping(value = "/courseTeachers/courseNames", method = RequestMethod.GET)
    public Result queryAllCourseName() {
        return courseTeacherService.queryAllCourseName();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有已分配的教师课程信息
     */
    @RequestMapping(value = "/courseTeachers/teacherAndCourses", method = RequestMethod.GET)
    public Result queryAllTeacherAndCourseInfo() {
        return courseTeacherService.queryAllTeacherAndCourseInfo();
    }
}
